package drops.gui;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import drops.dao.Patient;
import drops.dao.PatientPeer;
import drops.dao.Tooth;
import drops.dao.ToothPeer;

public class EditTeethPanel extends JPanel implements ActionListener, ItemListener{


	JLabel patientLabel;
	JLabel markingsLabel;
	JLabel notesLabel;
	JComboBox markingsChooserDropDown;
	JComboBox patientChooserDropDown;
	JComboBox toothChooserDropDown;
	JTextField notesTextField;
	JButton editButton;
	
	ComboBoxModel markingsModel;
	
	MainFrame mainFrame;
	int patientNumber;
	int toothNumber;
	
	public EditTeethPanel(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
		patientNumber = 1;	
		toothNumber = 1;
		notesLabel = new JLabel("Notes:");
		notesTextField = new JTextField();
		
		String[] markingsChooserList = { " ","X", "F", "HEAVY SHADE", "RC", "RF", "M", "DU", "MAL", "FLU", "Gn", "Gm", "Gs", "CMR", "()", "Xt", "xt", "Am", "Com", "JC", "I", "OP", "ZOE", "TF", "R" };
		markingsModel = new DefaultComboBoxModel(markingsChooserList);
		markingsChooserDropDown = new JComboBox(markingsModel);
		
		editButton = new JButton("Edit");
		editButton.addActionListener(this);
		
		
		setLayout(new GridLayout(3,2));
		//add(markingsLabel);
		add(markingsChooserDropDown);
		add(notesLabel);
		add(notesTextField);
		add(editButton);
	}
	
	
	private void updateSelect() {
		Tooth t = ToothPeer.searchTooth(toothNumber, patientNumber);
		if (t==null) return;
		markingsChooserDropDown.setSelectedIndex(t.getValue());
		notesTextField.setText(t.getNotes());
	}
	
	public void setTooth(int toothNumber) {
		this.toothNumber = toothNumber;
		updateSelect();
	}
	
	public void setPatient(int patientNumber) {
		this.patientNumber = patientNumber;
		updateSelect();
	}

	public void editTooth() {
		int value = markingsChooserDropDown.getSelectedIndex();
		String notes = notesTextField.getText();
		if (ToothPeer.editTooth(toothNumber, patientNumber, value, notes)) {
			JOptionPane.showMessageDialog(this, "Edit Successful");
			mainFrame.closeEditTeethFrame();
		}
		else {
			JOptionPane.showMessageDialog(this, "Error in editing");
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==editButton) {
			editTooth();
		}
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		updateSelect();
	}
	
}
